/**
 * 0092
 *  
 * @Author : Ruoyu Wang
 * @DateTime : 24-11-11 下午5:33
 */

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}

    ListNode(int x) : val(x), next(nullptr) {}

    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode *reverseBetween(ListNode *head, int left, int right) {
        ListNode *h = new ListNode(-1, head);
        ListNode *p = h;

        if (left == right) {
            return head;
        }

        ListNode *ll;
        ListNode *l;
        ListNode *r;
        ListNode *rr;
        for (int i = 0; i <= right; ++i) {
            if (i == left - 1) {
                ll = p;
            } else if (i == left) {
                l = p;
            } else if (i == right) {
                r = p;
            }
            p = p->next;
        }
        rr = p;
        ll->next = nullptr;
        r->next = nullptr;

        ListNode *a = nullptr;
        ListNode *b = l;
        ListNode *s;
        while (b != nullptr) {
            s = b->next;
            b->next = a;
            a = b;
            b = s;
        }

        ll->next = r;
        l->next = rr;
        return h->next;
    }
};

int main() {

    return 0;
}